<?php

class CampaignTable extends Doctrine_Table {

    /**
     * Returns active Campaigns, i.e. not deleted ones
     * @return Doctrine_Collection
     */
    public function getActiveCampaigns() {
        $q = $this->createQuery('c')->where('c.deleted_at is null ');
        return $q->execute();
    }

    /**
     * Returns open Campaigns, i.e. with status TO_DO and start_date <= now and
     * stop_date > now
     * @return Doctrine_Collection
     */
    public function getNextOpenCampaign() {
        $q = $this->createQuery('c');
        $q->where('c.status = ? and c.continuous = ? and c.start_date <= ? and (c.stop_date > ? or c.stop_date is null) ', array('TO_DO', 0, date('Y-m-d H:i:s', time()), date('Y-m-d H:i:s', time())));
        return $q->fetchOne();
    }

    /**
     * Returns a Doctrine_Collection of active continuous Campaigns
     * @return Doctrine_Collection
     */
    public function getContinuousCampaigns(){
        $q = $this->createQuery('c');
        $q->where('c.status = ? and c.continuous = ? and c.start_date <= ? and (c.stop_date > ? or c.stop_date is null) ', array('TO_DO', 1, date('Y-m-d H:i:s', time()), date('Y-m-d H:i:s', time())));
        return $q->execute();
    }
}
